//
//  QLKGuideView.m
//  CloudsOutpatient
//
//  Created by zzs on 15/6/16.
//  Copyright (c) 2015年 HC. All rights reserved.
//

#import "QLKGuideView.h"
#import "AppDelegate.h"
#import "UIImage+MultiFormat.h"
@implementation QLKGuideView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        scrolloview=[[UIScrollView alloc] initWithFrame:frame];
        scrolloview.pagingEnabled=YES;
        scrolloview.delegate=self;
        scrolloview.showsVerticalScrollIndicator=NO;
        scrolloview.bounces=NO;
        [self addSubview:scrolloview];
        [self initGuideImage];
    }
return self;
}
- (void)initGuideImage
{
    NSMutableArray* images = [NSMutableArray arrayWithObjects:@"guide1-568h",@"guide2-568h", @"guide3-568h",nil];
    if (IS_6Inch()) {
     images = [NSMutableArray arrayWithObjects:@"guide1-667h@2x.png",@"guide2-667h@2x.png", @"guide3-667h@2x.png",nil];
    }
    if (IS_6PInch()) {
        images = [NSMutableArray arrayWithObjects:@"guide1-667h@3x.png",@"guide2-667h@3x.png", @"guide3-667h@3x.png",nil];
    }
    [self addGuideImage:images];
}

-(void)addGuideImage:(NSArray *)imageArray
{
    _tempArray=imageArray;
    scrolloview.contentSize=CGSizeMake(SCREEN_WIDTH*imageArray.count, SCREEN_HEIGHT);
    for (int i=0; i<imageArray.count; i++) {
        UIImageView *imageview=[[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH*i, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        imageview.image=[UIImage imageSmartNamed:imageArray[i]];
        if (i==imageArray.count-1) {
            UIButton *bt=[UIButton buttonWithType:UIButtonTypeCustom];
            bt.frame=CGRectMake(55, 0, 262/2, 98/2);
            [bt setImage:[UIImage imageNamed:@"guangguang"] forState:UIControlStateNormal];
            bt.bottom=SCREEN_HEIGHT-60;
            bt.center=CGPointMake(self.width/2, bt.center.y);
            [bt addTarget:self action:@selector(tiyanAction:) forControlEvents:UIControlEventTouchUpInside];
            imageview.userInteractionEnabled=YES;
            [imageview addSubview:bt];
                                
        }
        [scrolloview addSubview:imageview];
    }
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    QLKDLog(@"%f",scrolloview.contentOffset.x);
    if (scrolloview.contentOffset.x==SCREEN_WIDTH*(_tempArray.count-1)) {
        [UIView animateWithDuration:0.5 animations:^{
            SharedAppDelegate.guideWindow.alpha=0.0;
        } completion:^(BOOL finished) {
             [self removeFromSuperview];
             [SharedAppDelegate.window makeKeyWindow];
        }];
      
       
    }
}
-(void)tiyanAction:(UIButton*)bt{
    [UIView animateWithDuration:0.5 animations:^{
        NSLog(@"SharedAppDelegate.guideWindow=%@",SharedAppDelegate.guideWindow);
        SharedAppDelegate.guideWindow.alpha=0.0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        [SharedAppDelegate.window makeKeyWindow];
    }];
}
@end
